package cn.com.dpc;

import java.lang.reflect.Field;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

public class MainTabActivity extends TabActivity {
	private static final int ABOUT_ID = 1;
	private TabHost mTabHost;
	private TabWidget tabWidget;

	private String districtList = "";

	Field mBottomLeftStrip;
	Field mBottomRightStrip;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 取消标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.tab_activity);

		// Bundle bundle = getIntent().getExtras();
		// districtList = bundle.getString("districtList").trim();

		// 获取数据
		initTabHost();
	}

	private View prepareTabView(Context context, int titleId, int drawable) {
		View view = LayoutInflater.from(context).inflate(R.layout.tab_main_nav,
				null);
		TextView tv = (TextView) view.findViewById(R.id.tvTitle);
		tv.setText(getText(titleId).toString());
		ImageView iv = (ImageView) view.findViewById(R.id.ivIcon);
		iv.setImageResource(drawable);
		return view;
	}

	private void initTabHost() {
		if (mTabHost != null) {
			throw new IllegalStateException(
					"Trying to intialize already initializd TabHost");
		}
		mTabHost = getTabHost();

		Intent dataTab = new Intent(this, DataActivity.class);
		Intent alarmTab = new Intent(this, AlarmActivity.class);
		Intent aboutTab = new Intent(this, AddActivity.class);
		// dataTab.putExtra("districtList", districtList);

		// 数据
		mTabHost.addTab(mTabHost
				.newTabSpec("one")
				.setIndicator(
						prepareTabView(mTabHost.getContext(), R.string.tab1,
								R.drawable.data)).setContent(dataTab));

		// 告警
		mTabHost.addTab(mTabHost
				.newTabSpec("two")
				.setIndicator(
						prepareTabView(mTabHost.getContext(), R.string.tab2,
								R.drawable.data)).setContent(alarmTab));

		// 关于
		mTabHost.addTab(mTabHost
				.newTabSpec("three")
				.setIndicator(
						prepareTabView(mTabHost.getContext(), R.string.tab3,
								R.drawable.more)).setContent(aboutTab));

		tabWidget = (TabWidget) findViewById(android.R.id.tabs);

		SetupTabs(mTabHost);

		mTabHost.setCurrentTab(1);

	}

	private void SetupTabs(TabHost tabHost) {

		if (Integer.valueOf(Build.VERSION.SDK) <= 7) {
			try {
				mBottomLeftStrip = tabWidget.getClass().getDeclaredField(
						"mBottomLeftStrip");
				mBottomRightStrip = tabWidget.getClass().getDeclaredField(
						"mBottomRightStrip");
				if (!mBottomLeftStrip.isAccessible()) {
					mBottomLeftStrip.setAccessible(true);
				}
				if (!mBottomRightStrip.isAccessible()) {
					mBottomRightStrip.setAccessible(true);
				}
				mBottomLeftStrip.set(tabWidget,
						getResources().getDrawable(R.drawable.no));
				mBottomRightStrip.set(tabWidget,
						getResources().getDrawable(R.drawable.no));

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				mBottomLeftStrip = tabWidget.getClass().getDeclaredField(
						"mLeftStrip");
				mBottomRightStrip = tabWidget.getClass().getDeclaredField(
						"mRightStrip");
				if (!mBottomLeftStrip.isAccessible()) {
					mBottomLeftStrip.setAccessible(true);
				}
				if (!mBottomRightStrip.isAccessible()) {
					mBottomRightStrip.setAccessible(true);
				}
				mBottomLeftStrip.set(tabWidget,
						getResources().getDrawable(R.drawable.no));
				mBottomRightStrip.set(tabWidget,
						getResources().getDrawable(R.drawable.no));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
